################################################################################
#* Licensed Materials - Property of IBM  
#*  
#* OCO Source Materials  
#*  
#* (C) Copyright IBM Corp. 2014 All Rights Reserved  
#*  
#* The source code for this program is not published or other-  
#* wise divested of its trade secrets, irrespective of what has  
#* been deposited with the U.S. Copyright Office.  
################################################################################

################################################################################
# Author:yushaoyang@cn.ibm.com
# Createdate:Tue May 19 23:08:50 2015
# Lastupd:Tue May 19 23:08:50 2015

# Description
# Updatelog:
# 1. "new this shell"
################################################################################

java_require 'rubysrc/irubyservice'
java_require 'rubysrc/libs/query'
java_package 'com.carepulse.query'

class RubyQueryService
  
  java_import "javax.servlet.http.HttpServletRequest"
  java_import "javax.servlet.http.HttpServletResponse"
  java_import "javax.servlet.ServletContext"

  java_signature "void init_service(String dbconfig_path)"
  def self.init_service dbcfgpath
    Query::ExportService::QueryService.init_service dbcfgpath
  end
  
  java_signature "RubyQueryService (ServletContext servletctx)"
  def initialize servletctx
    @service =Query::ExportService::QueryService.new
    @servletctx=servletctx
  end

  java_signature "void doGet(HttpServletRequest req, HttpServletResponse res)"
  def doGet req,res
    result,code=@service.doGet *parse_url(req)
    res.setStatus(code);
    res.getWriter().println(result.to_s) unless result.nil?
  end

  java_signature "void doPut(HttpServletRequest req, HttpServletResponse res)"
  def doPut req,res
    result,code=@service.doPut *parse_url(req)
    res.setStatus(code);
    res.getWriter().println(result.to_s) unless result.nil?
  end

  java_signature "void doPost(HttpServletRequest req, HttpServletResponse res)"
  def doPost req,res
    result,code=@service.doPost *parse_url(req)
    res.setStatus(code);
    res.getWriter().println(result.to_s) unless result.nil?
  end

  java_signature "void doDelete(HttpServletRequest req, HttpServletResponse res)"
  def doDelete req,res
    result,code=@service.doDelete *parse_url(req)
    res.setStatus(code);
    res.getWriter().println(result.to_s) unless result.nil?
  end

  def params req
    paramstr=req.getQueryString
    result=nil
    unless paramstr.nil?
    result=Hash[paramstr.split(/[&=]/).each_slice(2).to_a] 
    result[:paramstr]=paramstr
    end
    result
  end
  
  def data req
    String jb =""
    reader = req.getReader();
    while(not (line=reader.readLine()).nil?) 
      jb.concat(line);
    end 
    jb
  end

  def parse_url req
    uristr=req.getRequestURI
    uri=uristr.split("/")
    _url="/#{uri[2..-1].join("/")}"
    _qparams=params(req)
    _body=data(req)
    _header={} #improve it later
    log.debug "===============begin irubyservice==================="
    log.debug _url
    log.debug _qparams
    log.debug _body
    log.debug "===============end irubyservice==================="
    return _url,_qparams,_body,_header
  end
  
end
